package com.supreme.proxyserver.service;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.stereotype.Service;

@Service
public class ProxyFactoryService {

    interface IGunDam {

        void showName();
    }

    static class GunDam implements IGunDam {

        private final String name;

        public GunDam(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public void showName() {
            System.out.println("GunDam showName: " + this.name);
        }
    }

    static class ZGunDam implements IGunDam {

        private final String name;

        public ZGunDam(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public void showName() {
            System.out.println("ZGunDam showName: " + this.name);
        }
    }

    public void showGunDam() {
        final GunDam gunDam = new GunDam("高达");

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(gunDam);

        proxyFactory.addAdvice((MethodInterceptor) methodInvocation -> {
            final Object proceed = methodInvocation.proceed();
            return proceed;
        });

        IGunDam gunDamProxy = (GunDam) proxyFactory.getProxy();
        gunDamProxy.showName();


        ZGunDam zGunDam = new ZGunDam("Z高达");
        ProxyFactory proxyFactory2 = new ProxyFactory();
        proxyFactory2.setTarget(zGunDam);

        proxyFactory2.addAdvice((MethodInterceptor) methodInvocation -> {
            final Object proceed = methodInvocation.proceed();
            return proceed;
        });

        IGunDam zGunDamProxy = (ZGunDam) proxyFactory2.getProxy();
        zGunDamProxy.showName();
    }
}
